package com.ushine.web.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ushine.web.component.page.PageRequestQuery;
import com.ushine.web.log.mapper.SystemLogMapper;
import com.ushine.web.log.model.Log;
import com.ushine.web.log.service.SystemLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @Author: yls
 * @Date: 2020/10/19 23:54
 * @Description: 日志记录service层
 * @Version 1.0
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class SystemLogServiceImpl extends ServiceImpl<SystemLogMapper, Log> implements SystemLogService {

    @Override
    public Page<Log> listLogByPageHelper(PageRequestQuery<Log> pageRequestQuery) throws ParseException {
        Page<Log> page = new Page<>(pageRequestQuery.getPageNum(), pageRequestQuery.getPageSize());
        LambdaQueryWrapper<Log> queryWrapper = getSystemLogQueryWrapper();
        return page(page, queryWrapper);
    }

    /**
     * 功能描述:
     * 〈 组装日志查询工具 〉
     *
     * @return : com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.ushine.log.model.SystemLogDO>
     * @author : yls
     * @date : 2022/1/28 11:55
     */
    private LambdaQueryWrapper<Log> getSystemLogQueryWrapper() {
        LambdaQueryWrapper<Log> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.le(Log::getCreateTime, new Date());
        queryWrapper.orderByDesc(Log::getCreateTime);
        return queryWrapper;
    }

    @Override
    public Page<Log> listLogByPage(PageRequestQuery pageRequestQuery) throws ParseException {
        LambdaQueryWrapper<Log> systemLogQueryWrapper = getSystemLogQueryWrapper();
        Page<Log> page = new Page<>(pageRequestQuery.getPageNum(), pageRequestQuery.getPageSize());
        return page(page, systemLogQueryWrapper);
    }

    @Override
    public List<Log> listAllLogs() {
        LambdaQueryWrapper<Log> systemLogQueryWrapper = getSystemLogQueryWrapper();
        return list(systemLogQueryWrapper);
    }


}
